//
//  PresentSimpleRedViewController.m
//  UIViewControllerTransitionDemo
//
//  Created by Guo Rui on 2017/7/3.
//  Copyright © 2017年 GR Harry. All rights reserved.
//

#import "PresentSimpleRedViewController.h"
#import "PresentSimpleAnimator.h"
#import <UIKit/UIKit.h>

@interface PresentSimpleRedViewController ()<UIViewControllerTransitioningDelegate>
@property (strong, nonatomic) UIPercentDrivenInteractiveTransition *interactiveController;
@property (assign, nonatomic) BOOL interactiving;
@end

@implementation PresentSimpleRedViewController

- (instancetype)init
{
    self = [super init];
    if (self) {
        self.transitioningDelegate = self;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor redColor];
    
    UIButton *dismissButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [dismissButton setTitle:@"Dissmiss" forState:UIControlStateNormal];
    [dismissButton addTarget:self action:@selector(dismissAction) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:dismissButton];
    
    [dismissButton sizeToFit];
    dismissButton.center = self.view.center;
    
    self.interactiveController = [[UIPercentDrivenInteractiveTransition alloc] init];
    
    [self addGesture];
}

- (void)addGesture {
    UIPanGestureRecognizer *recognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)];
    [self.view addGestureRecognizer:recognizer];
}

- (void)pan:(UIPanGestureRecognizer *)recognizer {
    CGFloat x = [recognizer translationInView:recognizer.view].x;
    CGFloat progress = MAX(0, MIN(1, x / 200.f));
    
    NSLog(@"x - %lf, p - %lf", x, progress);
    
    switch (recognizer.state) {
        case UIGestureRecognizerStateBegan:
        {
            self.transitioningDelegate = self;
            self.interactiving = YES;
            [self dismissViewControllerAnimated:YES completion:nil];
        }
            break;
        case UIGestureRecognizerStateChanged:
        {
            [self.interactiveController updateInteractiveTransition:progress];
        }
            break;
        case UIGestureRecognizerStateCancelled:
        {
            self.interactiving = NO;
            [self.interactiveController cancelInteractiveTransition];
        }
            break;
        case UIGestureRecognizerStateEnded:
        {
            self.interactiving = NO;
            if (progress > .5f) {
                [self.interactiveController finishInteractiveTransition];
            }
            else {
                [self.interactiveController cancelInteractiveTransition];
            }
        }
            break;
        default:
            break;
    }
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    NSLog(@"viewWillAppear");
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    NSLog(@"viewDidAppear");
}

- (void)dismissAction {
    
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (id<UIViewControllerAnimatedTransitioning>)animationControllerForDismissedController:(UIViewController *)dismissed {
    return [[PresentSimpleAnimator alloc] initWithDismiss:YES];
}

- (id<UIViewControllerInteractiveTransitioning>)interactionControllerForDismissal:(id<UIViewControllerAnimatedTransitioning>)animator {
    return self.interactiving ? self.interactiveController : nil;
}

- (id<UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController *)presented presentingController:(UIViewController *)presenting sourceController:(UIViewController *)source {
    return [[PresentSimpleAnimator alloc] initWithDismiss:NO];
}

- (void)dealloc {
    
    NSLog(@"\n************\n %@ dealloc \n************", self.class);
}
@end
